/**
 * (created at 2011-1-23)
 */
package qiushuo.mysqlparser.ast.expression.primary;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import qiushuo.mysqlparser.ast.expression.Expression;
import qiushuo.mysqlparser.generator.SQLType;

/**
 * @author <a href="mailto:QiuShuo1985@gmail.com">QIU Shuo</a>
 */
public class RowExpression extends PrimaryExpression {
    private final List<Expression> rowExprList;

    public RowExpression(List<Expression> rowExprList) {
        if (rowExprList == null || rowExprList.size() <= 0) {
            this.rowExprList = Collections.emptyList();
        } else if (rowExprList instanceof ArrayList) {
            this.rowExprList = rowExprList;
        } else {
            this.rowExprList = new ArrayList<Expression>(rowExprList);
        }
    }

    @Override
    public String generateSql(SQLType type) {
        switch (type) {
        case MySQL:
            StringBuilder sb = new StringBuilder();
            sb.append("ROW(");
            boolean isFst = true;
            for (Expression expr : rowExprList) {
                if (isFst) {
                    isFst = false;
                } else {
                    sb.append(", ");
                }
                sb.append(expr.generateSql(type));
            }
            sb.append(")");
            return sb.toString();
        default:
            throw new UnsupportedOperationException("only mysql sql can be generated");
        }
    }
}
